package com.aiyige.page.filter.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.aiyige.model.commontag.CommonTag;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.NumberUtil;

import java.util.LinkedList;
import java.util.List;

public class Filter implements Parcelable {
    public static final int TYPE_MULTI_SELECT_SMALL_RECT = 1;
    public static final int TYPE_MULTI_SELECT_BIG_RECT = 2;
    public static final int TYPE_SINGLE_SELECT_SMALL_RECT = 3;
    public static final int TYPE_SINGLE_SELECT_BIG_RECT = 4;
    public static final int TYPE_ADDRESS = 5;
    public static final int TYPE_TIME_RANGE = 6;
    public static final int TYPE_UNKNOWN = 7;
    String id;
    String name;
    int type;
    List<CommonTag> tagList;
    String province;
    String city;
    String cityId;
    Long startTime;
    Long endTime;

    public Filter() {
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (this == obj) return true;
        if (!(obj instanceof Filter)) return false;
        Filter other = (Filter) obj;
        if (!this.id.equals(other.id)) return false;
        if (this.type != other.type) return false;
        switch (this.type) {
            case TYPE_MULTI_SELECT_BIG_RECT:
            case TYPE_MULTI_SELECT_SMALL_RECT:
            case TYPE_SINGLE_SELECT_BIG_RECT:
            case TYPE_SINGLE_SELECT_SMALL_RECT: {
                if (ListUtil.isEmpty(tagList) && ListUtil.isEmpty(other.tagList)) {
                    return true;
                } else if (ListUtil.isEmpty(tagList) || ListUtil.isEmpty(tagList)) {
                    return false;
                } else {
                    if (tagList.size() != other.tagList.size()) return false;
                    for (CommonTag item : tagList) {
                        if (!other.tagList.contains(item)) {
                            return false;
                        }
                    }
                    return true;
                }
            }
            case TYPE_ADDRESS: {
                if (StringUtils.safeEquals(city, other.city) && StringUtils.safeEquals(province, other.province) && StringUtils.safeEquals(cityId, other.cityId)) {
                    return true;
                } else {
                    return false;
                }
            }
            case TYPE_TIME_RANGE: {
                if (NumberUtil.safeEquals(startTime, other.startTime) && NumberUtil.safeEquals(endTime, other.endTime)) {
                    return true;
                } else {
                    return false;
                }
            }
            default: {
                return true;
            }
        }
    }

    private Filter(Builder builder) {
        setId(builder.id);
        setName(builder.name);
        setType(builder.type);
        setTagList(builder.tagList);
        setProvince(builder.province);
        setCity(builder.city);
        setCityId(builder.cityId);
        setStartTime(builder.startTime);
        setEndTime(builder.endTime);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<CommonTag> getTagList() {
        return tagList;
    }

    public void setTagList(List<CommonTag> tagList) {
        this.tagList = tagList;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCityId() {
        return cityId;
    }

    public void setCityId(String cityId) {
        this.cityId = cityId;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }


    public static final class Builder {
        private String id;
        private String name;
        private int type;
        private List<CommonTag> tagList;
        private String province;
        private String city;
        private String cityId;
        private Long startTime;
        private Long endTime;

        private Builder() {
            id = "";
            name = "";
            type = TYPE_UNKNOWN;
            tagList = new LinkedList<>();
            province = "";
            city = "";
            cityId = "";
            startTime = null;
            endTime = null;
        }

        public Builder id(String val) {
            id = val;
            return this;
        }

        public Builder name(String val) {
            name = val;
            return this;
        }

        public Builder type(int val) {
            type = val;
            return this;
        }

        public Builder tagList(List<CommonTag> val) {
            tagList = val;
            return this;
        }

        public Builder province(String val) {
            province = val;
            return this;
        }

        public Builder city(String val) {
            city = val;
            return this;
        }

        public Builder cityId(String val) {
            cityId = val;
            return this;
        }

        public Builder startTime(Long val) {
            startTime = val;
            return this;
        }

        public Builder endTime(Long val) {
            endTime = val;
            return this;
        }

        public Filter build() {
            return new Filter(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.name);
        dest.writeInt(this.type);
        dest.writeTypedList(this.tagList);
        dest.writeString(this.province);
        dest.writeString(this.city);
        dest.writeString(this.cityId);
        dest.writeValue(this.startTime);
        dest.writeValue(this.endTime);
    }

    protected Filter(Parcel in) {
        this.id = in.readString();
        this.name = in.readString();
        this.type = in.readInt();
        this.tagList = in.createTypedArrayList(CommonTag.CREATOR);
        this.province = in.readString();
        this.city = in.readString();
        this.cityId = in.readString();
        this.startTime = (Long) in.readValue(Long.class.getClassLoader());
        this.endTime = (Long) in.readValue(Long.class.getClassLoader());
    }

    public static final Parcelable.Creator<Filter> CREATOR = new Parcelable.Creator<Filter>() {
        @Override
        public Filter createFromParcel(Parcel source) {
            return new Filter(source);
        }

        @Override
        public Filter[] newArray(int size) {
            return new Filter[size];
        }
    };
}
